package com.myutils;

import android.app.Application;
import android.content.Context;

import androidx.appcompat.app.AppCompatActivity;

import com.reader.base.ReaderBase;
import com.reader.helper.ISO180006BOperateTagBuffer;
import com.reader.helper.InventoryBuffer;
import com.reader.helper.OperateTagBuffer;
import com.reader.helper.ReaderHelper;
import com.reader.helper.ReaderSetting;

/**
 * 类描述：UHF帮助类<br/>
 * 创建人：lwj<br/>
 * 创建时间：2019/8/13 15:22 <br/>
 */
public class UHFHelp {
    private HomeAndRecentAppsKeyReceiver mReceiver;
    private UHFUtils mUhfUtils;

    private UHFHelp() {
    }

    public static UHFHelp newInstance() {
        return new UHFHelp();
    }

    /**
     * 必须先初始化readHelper，在application中
     * 初始化ReadHelper
     */
    public static void initReaderHelper(Application application) {
        try {
            ReaderHelper.setContext(application);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接
     */
    public static void closeConnect() {
        UHFConnectUtils.close();

        /*if (BluetoothAdapter.getDefaultAdapter() != null) {
            BluetoothAdapter.getDefaultAdapter().disable();//关闭蓝牙
        }*/
    }


    /////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * 初始化连接，在需要的地方初始化连接
     */
    public void initConnectUtils(Application application) {
        //1.接出第一步 初始化连接
        UHFConnectUtils uhfConnectUtils = UHFConnectUtils.newInstance();
        uhfConnectUtils.onCreate(application);
    }

    /**
     * 添加 home键 和 recent_apps键 监听
     */
    public void onStart(AppCompatActivity activity) {
        //2.接出第二步 添加 home键 和 recent_apps键 监听
        if (mReceiver == null) {
            mReceiver = HomeAndRecentAppsKeyReceiver.newInstance();
        }
        mReceiver.onStart(activity);
    }

    public void onDestroy(AppCompatActivity activity) {
        if (mReceiver != null) {
            //第二步，结束时 移除广播
            mReceiver.unregisterReceiver(activity);
        }

        if (mUhfUtils != null) {
            mUhfUtils.onDestroy();
        }
        mUhfUtils = null;
    }
    ////////////////////////////////////////////////////////////////////////////////

    /**
     * 初始化UHF，获取uhf 工具类，具有扫码监听 一些逻辑处理
     */
    public void initUHF() {
        //初始化UHF
        if (mUhfUtils == null) {
            mUhfUtils = UHFUtils.newInstance();
            mUhfUtils.init();
        }
    }

    public void setScanCallback(UHFUtils.ScanCallback scanCallback) {
        if (mUhfUtils != null) {
            mUhfUtils.setScanCallback(scanCallback);
        }
    }

    public void setSetCallback(UHFUtils.SetCallback callback) {
        if (mUhfUtils != null) {
            mUhfUtils.setSetCallback(callback);
        }
    }

    /**
     * 调用的地方写
     */
    public void onResume() {
        if (getReader() != null) {
            if (!getReader().IsAlive())
                getReader().StartWait();
        }
    }

    /**
     * 调用的地方写
     */
    public void onPause() {
        if (isKeyPressing()) {
            startStopBt(false);
        }
    }

    //需要调用获取扫码结果的地方使用
    public ReaderBase getReader() {
        return mUhfUtils.getReader();
    }

    public ReaderHelper getReaderHelper() {
        return mUhfUtils.getReaderHelper();
    }

    public static ReaderSetting getM_curReaderSetting() {
        return UHFUtils.getM_curReaderSetting();
    }

    public static InventoryBuffer getM_curInventoryBuffer() {
        return UHFUtils.getM_curInventoryBuffer();
    }

    public static OperateTagBuffer getM_curOperateTagBuffer() {
        return UHFUtils.getM_curOperateTagBuffer();
    }

    public static ISO180006BOperateTagBuffer getM_curOperateTagISO18000Buffer() {
        return UHFUtils.getM_curOperateTagISO18000Buffer();
    }

    /**
     * 在 onDestroy中取消注册
     */
    public void addScanCodeReceiver(Context context) {
        if (mUhfUtils != null) {
            mUhfUtils.addScanCodeReceiver(context);
        }
    }

    public void addUhfSetReceiver(Context context) {
        if (mUhfUtils != null) {
            mUhfUtils.addUhfSetReceiver(context);
        }
    }

    public void unregisterUhfSetReceiver(Context context) {
        if (mUhfUtils != null) {
            mUhfUtils.addUhfSetReceiver(context);
        }
    }

    public void clearList() {
        if (mUhfUtils != null) {
            mUhfUtils.clearList();
        }
    }

    public boolean isKeyPressing() {
        if (mUhfUtils == null) {
            return false;
        }
        return mUhfUtils.mKeyPressing;
    }

    public void startStopBt(boolean isStart) {
        if (mUhfUtils != null) {
            mUhfUtils.startStopBt(isStart);
        }
    }
}
